package com.topvision.myviewtest.kotlin.part1

/**
 * @FileName : Test5.kt
 * @Author   : Zero_Tzz
 * @E-mail   : 497847377@qq.com
 * @Data     : 2018-05-31 21:55
 * @Describe :
 */

// 返回When表达式
fun transform(color: String): Int {
    return when (color) {
        "Red" -> 0
        "Blue" -> 1
        "Yellow" -> 2
        else -> -1
    }
}

// try catch表达式
fun test() {
    val result = try {
        // 写内容

    } catch (e: Exception) {
        throw IllegalArgumentException("error")
    }
    // 在这里可以使用result 否则会直接catch错误
}

// if表达式
fun foo(x: Int): String {
    val result = if (x == 1) {
        "one"
    } else if (x == 2) {
        "two"
    } else {
        "error"
    }
    return result
}

fun main(args: Array<String>) {
    print(transform("Red"))
    print(transform("Blue"))
    print(transform("Yellow"))
    print(transform("a"))
//    out:0 1 2 -1
    println("--------------------------")

    print(foo(1))
    print(foo(2))
    print(foo(3))
}
